package com.example.bee_work_3.collect;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.bee_work_3.R;
import com.example.bee_work_3.collect.ListActivity;

import java.util.List;

public class ListActivityAdapter extends ArrayAdapter<List<String[]>> {
    private int mResourceId;
    private List<String[]> dataList;
    private Context mContext;
    private ListActivity.CallBack callBack;

    public ListActivityAdapter(Context context, int resource, List<String[]> list, ListActivity.CallBack callBack) {
        super(context, resource);
        this.dataList = list;
        this.mContext = context;
        this.mResourceId = resource;
        this.callBack = callBack;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    /**
     * 0: id
     * 1: author
     * 2: title
     * 3: niceDate
     * 4: link
     */
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView!=null){
            return convertView;
        }
        final String[] item = dataList.get(position);
        View view = LayoutInflater.from(mContext).inflate(mResourceId, null);
        TextView content = view.findViewById(R.id.content);
        TextView detail = view.findViewById(R.id.detail);
        LinearLayout button = view.findViewById(R.id.button);
        Button delete = view.findViewById(R.id.delete);

        content.setText(item[2]);
        detail.setText("作者：" + item[1] + "收藏时间" + item[3]);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.onItemClick(item);
            }
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.onDelete(Integer.parseInt(item[0]), position);
            }
        });
        return view;
    }
}
